﻿/* File User
 * Gets files inrange and turns them on if applicable,
 * gets the file used if play presses activate key
 * 
 * Its ment to go onto the players camera
 */

using UnityEngine;
using System.Collections;
using System.Collections.Generic;



public class FileUser : MonoBehaviour {
	public GameObjectManager GOM;
	public bool b_UpdateSphere;
	public bool b_ColorActiveObjects;
	public float f_Range;
	public float f_Delay;
	private float f_Time;
	public GameObject[] LGO_ActiveObjInSphere_LastTime;
	public List<GameObject> LGO_ActiveObjInSphere_ThisTime;
	public KeyCode kc_Use = KeyCode.F;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(b_UpdateSphere){
			f_Time += Time.deltaTime;
			if(f_Time >= f_Delay){
				SphereUpdate();
				f_Time = 0;
			}
		}
		if(Input.GetKeyDown(kc_Use)){
			GameObject UsedGameObject = RayCastReturnBaseFile(this.gameObject);
			if(UsedGameObject){
				if(UsedGameObject.GetComponent<BaseFile>()){
					BaseFile BaseFileInfo = UsedGameObject.GetComponent<BaseFile>();
					//did you use just a image
					if(BaseFileInfo.FileType == GameObjectManager.FileTypes.Image){
						UsedGameObject.GetComponent<ImageFile>().LoadFullImage();

					}
					//did you use just a folder
					if(BaseFileInfo.FileType == GameObjectManager.FileTypes.Folder){
						GOM.s_FV_CurDir = UsedGameObject.GetComponent<BaseFile>().FullPath;
						GOM.Setup();
					}
				}
			}
		}
	}
	public GameObject RayCastReturnBaseFile(GameObject Source){
		Ray FireRay = new Ray(Source.transform.position,Source.transform.forward);
		RaycastHit FireRayHit;
		if(Physics.Raycast(FireRay, out FireRayHit , f_Range)){
			if(FireRayHit.collider.gameObject.name == "Col"){
				//print("HIT:"+FireRayHit.collider.transform.parent.gameObject.name);
				return FireRayHit.collider.transform.parent.gameObject;
			}
		}
		return null;
	}
	void SphereUpdate(){
		LGO_ActiveObjInSphere_LastTime = new GameObject[LGO_ActiveObjInSphere_ThisTime.Count];
		LGO_ActiveObjInSphere_ThisTime.CopyTo(LGO_ActiveObjInSphere_LastTime);
		LGO_ActiveObjInSphere_ThisTime.Clear();

		Collider[] ColArray = Physics.OverlapSphere(this.transform.position,f_Range);
		foreach(Collider C in ColArray){
			if(C.gameObject.name == "Col"){
				GameObject File = C.gameObject.transform.parent.gameObject;
				BaseFile BaseFileInfo = File.GetComponent<BaseFile>();
				if(b_ColorActiveObjects){
					File.renderer.material.color = Color.green;
				}
				if(BaseFileInfo.FileType == GameObjectManager.FileTypes.Image){
					File.GetComponent<ImageFile>().Refresh();

				}
				if(BaseFileInfo.FileType == GameObjectManager.FileTypes.Folder){
					File.GetComponent<RotateTowards>().Rotate();
				}

				LGO_ActiveObjInSphere_ThisTime.Add(File);
			}
		}


		//tell all the old boxes to unload and clear if there not in the new cast
		for(int OldListIndex = 0; OldListIndex < LGO_ActiveObjInSphere_LastTime.Length; OldListIndex++){
			bool StillInRange = false;
			for(int NewListIndex = 0; NewListIndex < LGO_ActiveObjInSphere_ThisTime.Count; NewListIndex++){
				//if the last frames list of gameobjects @ OldListIndex of it isnt in the new list at all unload it
				if(LGO_ActiveObjInSphere_LastTime[OldListIndex] == LGO_ActiveObjInSphere_ThisTime[NewListIndex]){
					StillInRange = true;
					break;
				}
			}
			if(!StillInRange){
				GameObject File = LGO_ActiveObjInSphere_LastTime[OldListIndex];
				BaseFile BaseFileInfo = File.GetComponent<BaseFile>();

				if(b_ColorActiveObjects){
					File.renderer.material.color = Color.white;
				}

				if(BaseFileInfo.FileType == GameObjectManager.FileTypes.Image){
					File.GetComponent<ImageFile>().Setup();
				}
				if(BaseFileInfo.FileType == GameObjectManager.FileTypes.Folder){
					File.GetComponent<RotateTowards>().Reset();
				}

			}
		}

	}
}
